package com.spzx.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.spzx.product.domain.Category;
import com.spzx.product.mapper.CategoryMapper;
import com.spzx.product.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * ClassName:CategoryServiceImpl
 * Package:com.spzx.product.service.impl
 * Description:
 *
 * @Author:helei
 * @Create:2024/9/20 - 14:18
 */
@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category>
        implements CategoryService {

    @Autowired
    private CategoryMapper categoryMapper;

    //获取商品分类下拉树列表
    @Override
    public List<Category> treeSelect(Long parentId) {
        LambdaQueryWrapper<Category> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Category::getParentId, parentId);
        List<Category> categoryList = this.list(wrapper);
        wrapper.clear();
        categoryList.stream().forEach(category -> {
            wrapper.eq(Category::getParentId, category.getId());
            List<Category> list = this.list(wrapper);
            if (list.size() > 0) {
                category.setHasChildren(true);
            } else {
                category.setHasChildren(false);
            }
        });
        return categoryList;
    }
}
